// 创建slice
import { createSlice, createAsyncThunk } from '@reduxjs/toolkit'

export const asyncLang = createAsyncThunk('yyy', (lang: string) => {
  // new Promise<类型>中泛型是描述resolve中传入的值的类型
  return new Promise<string>((resolve) => {
    //执行异步
    setTimeout(() => {
      resolve(lang)
    }, 1000)
  })
})
export default createSlice({
  name: 'xxx',
  initialState: { lang: 'zh', zzz: '123' },
  reducers: {
    setLang(state, action) {
      state.lang = action.payload
    },
  },
  extraReducers(builder) {
    builder.addCase(asyncLang.fulfilled, (state, action) => {
      // 当异步成功的时候触发这个函数
      state.lang = action.payload
    })
  },
})
